package MediaEntity;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import javax.swing.*;

/**
 * Java 播放音频
 * @ClassName: MusicPlayer
 * @Description: TODO
 * @author: hyacinth
 * @date: 2020年3月5日 上午12:10:53
 * @Copyright: hyacinth
 */
public class MusicPlayer {

    static Player player = null;

    public static void main(String[] args) throws FileNotFoundException, JavaLayerException {
        /*File file = new File("C:\\Users\\12967\\Music\\Breakbot _ Irfane - Baby I'm Yours.mp3");
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream stream = new BufferedInputStream(fis);
        Player player = new Player(stream);
        player.play();*/
        JFrame frame1=new JFrame("window");
        JPanel t=new JPanel();
        frame1.add(t);
        musicThread music=new musicThread("C:\\Users\\12967\\Music\\Breakbot _ Irfane - Baby I'm Yours.mp3");
        JButton chooseFile=new JButton("file");
        JButton pause=new JButton("pause");
        JButton start=new JButton("start");
        JButton restart=new JButton("restart");

        pause.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                music.pause();
            }
        });
        start.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                music.start();
            }
        });
        restart.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                music.restart();
            }
        });
        chooseFile.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JFrame choose=new JFrame("file");
                JFileChooser t=new JFileChooser();
                choose.add(t);
                choose.setVisible(true);
            }
        });
        t.add(start);
        t.add(pause);
        t.add(restart);
        t.add(chooseFile);
        frame1.setVisible(true);
    }

    /**
     * 播放 20 秒并结束播放
     */
    public void play() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    File file = new File("C:\\Users\\12967\\Music\\Breakbot _ Irfane - Baby I'm Yours.mp3");
                    FileInputStream fis = new FileInputStream(file);
                    BufferedInputStream stream = new BufferedInputStream(fis);
                    player = new Player(stream);
                    player.play();
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }).start();
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        player.close();
    }


}
